package com.key.w8.common.execute;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: k8
 * @CreateTime: 2025-03-09
 * @Version: 1.0
 */
public class ExecuteHandlerChain {
    private List<ChainExecuteHandler> executeHandlers = new ArrayList<>();
    public void addExecuteHandler(ChainExecuteHandler handler){
        handler.setChain(this);
        handler.setIndex(executeHandlers.size());
        this.executeHandlers.add(handler);
    }
    public Object invoke(Object msg,Object attach) throws Throwable{
        return executeHandlers.get(0).onInvoke(msg,attach);
    }
    ChainExecuteHandler next(int index){
        if (index < executeHandlers.size()){
            return executeHandlers.get(index);
        }
        return null;
    }
}
